﻿using System.Collections;
using System.Xml;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;


namespace Marbles.Core.Web.MVC.Enclosures
{
    public class EnclosureFileParser : IEnumerable
    {

        public static string EnclosureFilePath
        {
            get
            {
                return HttpContext.Current.Server.MapPath("~/App_Data/Enclosures.xml");
            }
        }

        public Enclosure this[int index]
        {
            get { return circuits[index] as Enclosure; }
        }

        public Enclosure this[string Name]
        {
            get { return GetEnclosure(Name); }
        }

        public int Count
        {
            get { return circuits.Count; }
        }

        private ArrayList circuits = null;


        public EnclosureFileParser()
        {
            this.circuits = new ArrayList();
            LoadCircuitFile(EnclosureFileParser.EnclosureFilePath);
        }

        public EnclosureFileParser(string EnclosureFilePath)
        {
            this.circuits = new ArrayList();
            LoadCircuitFile(EnclosureFilePath);
        }

        public Enclosure GetEnclosure(string Name)
        {
            foreach (Enclosure circuit in this.circuits)
            {
                if (circuit.Name == Name.ToLower().Trim()) { return circuit; }
            }

            return null;
        }

        public bool Contains(string Name)
        {
            foreach (Enclosure circuit in this.circuits)
            {
                if (circuit.Name == Name.ToLower().Trim()) { return true; }
            }

            return false;
        }

        public int IndexOf(string Name)
        {
            int i = 0;
            foreach (Enclosure circuit in this.circuits)
            {
                if (circuit.Name == Name.ToLower().Trim()) { return i; }
                i += 1;
            }


            return -1;
        }

        private void LoadCircuitFile(string filename)
        {
            if (!File.Exists(filename)) { return; }
            XmlDocument circuitFile = new XmlDocument();
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read);
            XmlNodeList circuitNodes = null;
            circuitFile.Load(fs);
            fs.Flush();
            fs.Close();
            this.circuits.Clear();
            circuitNodes = circuitFile.GetElementsByTagName("Enclosures");

                        
            if (circuitNodes.Count > 0)
            {
                for (int i = 0; i < circuitNodes[0].ChildNodes.Count; i++)
                {
                    XmlNode node = circuitNodes[0].ChildNodes[i];
                    if (node.Name == "Enclosure")
                    {
                        Enclosure item = null;
                        bool Live = true;
                        if (node.Attributes["Live"] != null)
                        {
                            if (!bool.TryParse(node.Attributes["Live"].Value, out Live)) { Live = true; }                            
                            item = new Enclosure(node.Attributes["Name"].Value, node.Attributes["Model"].Value, node.Attributes["View"].Value, Live);                            
                            this.circuits.Add(item);
                        }
                    }
                }
            }
        }

        public IEnumerator GetEnumerator()
        {
            return this.circuits.GetEnumerator();
        }
    }
}
